{% extends "base.html" %}

{% block title %}管理员登录 - Excel2User{% endblock %}

{% block content %}
<div class="container">
    <div class="header">
        <h1>🔐 管理员登录</h1>
        <p>请输入管理员账号和密码</p>
    </div>
    
    <div class="alert"></div>
    
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>
        
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>
        
        <button type="submit" class="btn">🚀 登录</button>
    </form>
    
    <div style="margin-top: 20px; text-align: center;">
        <a href="/" style="color: #667eea; text-decoration: none;">← 返回首页</a>
    </div>
    
    <div style="margin-top: 30px; padding: 15px; background: #f8f9fa; border-radius: 8px; font-size: 14px; color: #666;">
        <strong>默认账号信息：</strong><br>
        用户名: admin<br>
        密码: admin123
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.getElementById('loginForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const username = document.getElementById('username').value.trim();
    const password = document.getElementById('password').value.trim();
    
    if (!username || !password) {
        showAlert('请输入用户名和密码');
        return;
    }
    
    try {
        const response = await fetch('/admin/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ username, password })
        });
        
        const data = await response.json();
        
        if (data.success) {
            showAlert('登录成功，正在跳转...', 'success');
            setTimeout(() => {
                window.location.href = '/admin/dashboard';
            }, 1000);
        } else {
            showAlert(data.message);
        }
    } catch (error) {
        showAlert('登录失败，请稍后重试');
        console.error('Error:', error);
    }
});

// 支持回车键登录
document.addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        document.getElementById('loginForm').dispatchEvent(new Event('submit'));
    }
});
</script>
{% endblock %}